package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/31 9:30
 * @Version 1.0
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    /**
     * 秒杀列表
     * @param model
     * @return
     */
    @GetMapping("seckill.html")
    public String seckillList(Model model){
        //  获取远程调用数据
        Result result = activityFeignClient.findAll();
        //  存储数据 ${list}
        model.addAttribute("list",result.getData());
        //  返回秒杀列表
        return "seckill/index";
    }
    //  /seckill/'+${item.skuId}+'.html'

    /**
     * 秒杀详情控制器
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId,Model model){
        //  远程调用获取数据.
        Result result = this.activityFeignClient.getSeckillGoods(skuId);
        //  存储数据
        model.addAttribute("item",result.getData());
        //  返回秒杀详情页面。
        return "seckill/item";
    }

    //  排队页面
    //  http://activity.gmall.com/seckill/queue.html?skuId=25&skuIdStr=c81e728d9d4c2f636f067f89cc14862c
    @GetMapping("/seckill/queue.html")
    public String queue(HttpServletRequest request){
        //  存储skuId，抢购码
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));
        return "seckill/queue";
    }

    /**
     * 订单结算页
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String seckillTrade(Model model){
        //  userAddressList, detailArrayList, totalNum, totalAmount;
        //  从远程调用获取数据
        Result<Map> result = this.activityFeignClient.trade();
        if (result.isOk()){
            model.addAllAttributes(result.getData());
            //  返回订单结算页面。
            return "seckill/trade";
        } else {
            model.addAttribute("message","下订单失败.");
            return "seckill/fail";
        }
    }
}
